<?php
namespace app\admin\controller;
use think\Db;
class Base extends \think\Controller
{   
    public $userinfo;
    public function __construct(){
        parent::__construct();
        $this->_checkLogin();
        $this->_appConfig();
    }
    /**
     * 验证登录
     */
    private function _checkLogin(){
        //判断路由
        if(com_controller()=='publics')
            return;

        //判断登陆
        $adminUser = new \app\admin\model\AdminUser();
        if( !$adminUser->isLogin() )
            r(url("Publics/logOut"));
        
        $this->_global();
        //判断权限
        $role = new \app\admin\model\AdminRole();
        if( !$role->checkRight() ){
            com_error(['没有权限']);
            com_check();   
        }
        //左边导航
        $menu = $role->getMenu();
        $this->assign("menu", $menu);
    }
    
    /**
     * 如果登录，将用户信息写入全局变量
     */
    private function _global(){
        $id = session("admin_user_id");
        if( $id ){
            $this->userinfo = Db::name('admin_user')->find($id);
            $this->assign('userinfo', $this->userinfo);
            \my\G::set('admininfo', $this->userinfo);
        }
    }
    
    /**
     * 项目配置赋值
     */
    private function _appConfig(){
        $this->assign('sys',\app\common\model\Sys::get());
    }
}